import { stringify } from 'qs';
import request from '@/utils/request';

export async function queryProduct(params) {
  return request(`/api/devices/products?${stringify(params)}`);
}

export async function queryDeviceCount(params) {
  return request(`/api/devices/count?${stringify(params)}`);
}

export async function queryDevice(params) {
  return request(`/api/devices?${stringify(params)}`);
}

export async function del(params) {
  return request(`/api/devices/${params}`, {
    method: 'DELETE',
  });
}

export async function add(params) {
  return request('/api/devices', {
    method: 'POST',
    body: {
      ...params,
    },
  });
}

export async function batch(params) {
  return request('/api/devices/batch', {
    method: 'POST',
    body: {
      ...params,
    },
  });
}

export async function queryGroups() {
  return request('/api/devices/groups');
}

export async function queryDetail(params) {
  return request(`/api/devices/${params}`);
}

export async function edit(params) {
  return request('/api/devices', {
    method: 'PUT',
    body: {
      ...params,
    },
  });
}

export async function queryTopics(params) {
  return request(`/api/devices/${params}/topics`);
}

export async function delTopic(params) {
  return request(`/api/devices/topic/${params}`, {
    method: 'DELETE',
  });
}

export async function addTopic(params) {
  return request('/api/devices/topic', {
    method: 'POST',
    body: {
      ...params,
    },
  });
}

export async function addChildDevice(params) {
  return request('/api/devices/children', {
    method: 'POST',
    body: {
      ...params,
    },
  });
}
export async function queryDeviceList(params) {
  return request(`/api/devices/list?${stringify(params)}`);
}
export async function delChildDev(params) {
  return request(`/api/devices/children/${params}`, {
    method: 'DELETE',
  });
}
export async function queryDeviceExtend(params) {
  return request(`/api/devices/${params}/extend`);
}
export async function saveOrEditDeviceExtend(params) {
  return request('/api/devices/extend', {
    method: 'POST',
    body: {
      ...params,
    },
  });
}
export async function queryDeviceProps(params) {
  return request(`/api/devices/${params}/props`);
}
export async function queryDeviceServices(params) {
  return request(`/api/devices/${params}/services`);
}
export async function serviceExecute(params) {
  return request(`/api/devices/${params.deviceId}/services/${params.serviceId}/execute`, {
    method: 'POST',
    body: params.serviceParamList,
  });
}

export async function qryProtocolInfo(params) {
  return request(`/api/devices/${params}/protocol`);
}

export async function editProtocol(params) {
  return request(`/api/devices/protocol`, {
    method: 'PUT',
    body: {
      ...params,
    },
  });
}

export async function queryDecoder() {
  return request(`/api/products/decoder`);
}

export async function queryEncoder() {
  return request(`/api/products/encoder`);
}

export async function queryModbusProtocol(params) {
  return request(`/api/protocol/${params}/modbus`);
}
export async function queryModbusDataType() {
  return request(`/api/protocol/modbus/datatypes`);
}
export async function queryRegisterType() {
  return request(`/api/protocol/modbus/registertypes`);
}
export async function addModbusProtocol(params) {
  return request('/api/protocol/modbus', {
    method: 'POST',
    body: {
      ...params,
    },
  });
}
export async function delModbusProtocol(params) {
  return request(`/api/protocol/modbus/${params}`, {
    method: 'DELETE',
  });
}
export async function editModbusProtocol(params) {
  return request('/api/protocol/modbus', {
    method: 'PUT',
    body: {
      ...params,
    },
  });
}
export async function queryModbusDetail(params) {
  return request(`/api/protocol/modbus/${params}`);
}
